package com.neusoft.di.dibuy.controller;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.server.RouterFunction;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.reactive.function.server.ServerResponse;
import reactor.core.publisher.Mono;

import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.web.reactive.function.server.RequestPredicates.*;
import static org.springframework.web.reactive.function.server.RouterFunctions.route;

@Configuration
public class OrderController {

    @Bean
    public RouterFunction<ServerResponse> monoRouterFunction(OrderHandler orderHandler) {
        return route(GET("/{user}").and(accept(APPLICATION_JSON)), orderHandler::getUser)
                .andRoute(GET("/{user}/customers").and(accept(APPLICATION_JSON)), orderHandler::getUserCustomers)
                .andRoute(DELETE("/{user}").and(accept(APPLICATION_JSON)), orderHandler::deleteUser);

    }

    @Component
    public static class OrderHandler {
        public Mono<ServerResponse> getUser(ServerRequest request) {
            return Mono.deferContextual(context -> Mono.just(context.get("user")));
        }

        public Mono<ServerResponse> getUserCustomers(ServerRequest request) {
            return Mono.deferContextual(context -> Mono.just(context.get("user")));
        }

        public Mono<ServerResponse> deleteUser(ServerRequest request) {
            return Mono.deferContextual(context -> Mono.just(context.get("user")));
        }

    }

    public static class Animal {
        private String name = "Dog";

        public void setName(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }
    }

}
